//
//  ViewControllerForTravel.m
//  ProjectA
//
//  Created by dllo on 16/3/9.
//  Copyright © 2016年 刘旺辉. All rights reserved.
//

#import "ViewControllerForTravel.h" //C
#import "ModelForTravelTabViewHeader.h"  //M
#import "ModelForTravelTabViewSectionHeader.h"
#import "ModelForTravelTabViewCell.h"

#import "ViewForTravelTabViewHeader.h"  //V
#import "CellForTravelTabView.h"
#import "ViewForTravelTabViewSectionHeader.h"
@interface ViewControllerForTravel ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) ViewForTravelTabViewHeader *viewForTravelTabViewHeader;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *arrForTabViewSectionHeader;
@property (nonatomic, strong) NSMutableDictionary *dicForTabViewCell;


@end

@implementation ViewControllerForTravel
- (void)dealloc {

    self.tableView.dataSource = nil;
    self.tableView.delegate = nil;
    
    [_viewForTravelTabViewHeader release];
    [_imageView release];
    [_tableView release];
    
    [_arrForTabViewSectionHeader release];
    [_dicForTabViewCell release];
    
    [_url release];

    [super dealloc];

}
- (void)setLightDayModel {
    [super setLightDayModel];
    self.navigationItem.leftBarButtonItem.tintColor = RGBA(172, 135, 79, 1);
    self.barBackgroundView.backgroundColor = RGBA(255, 251, 222, 1);
    
}
- (void)setNightDayModel {
    [super setNightDayModel];
    self.navigationItem.leftBarButtonItem.tintColor = L_COLOR_WHITE;
    self.barBackgroundView.backgroundColor = RGBA(52, 52, 52, 1);
}
- (void)viewWillAppear:(BOOL)animated {
    if (ISNIGHT) {
        [self setNightDayModel];
    } else {
        [self setLightDayModel];
    }
}
- (void)viewWillDisappear:(BOOL)animated {
    self.barBackgroundView.backgroundColor = L_COLOR_CLEAR;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self createTableView];
    
    [self handleData];
    
    [self settingsNavigationController];
    
}
- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    [self.view addSubview:self.tableView];
    [_tableView release];
    self.tableView.backgroundColor = L_COLOR_CLEAR;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView.contentInset = UIEdgeInsetsMake(200, 0, 0, 0);
    
    [self.tableView registerClass:[CellForTravelTabView class] forCellReuseIdentifier:@"pool"];
    [self.tableView registerClass:[ViewForTravelTabViewSectionHeader class] forHeaderFooterViewReuseIdentifier:@"poolForHeader"];
    
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -200, self.view.frame.size.width, 200)];
    [self.tableView addSubview:self.imageView];
   // self.imageView.backgroundColor = [UIColor redColor];
    self.imageView.contentMode = UIViewContentModeScaleAspectFill;
    [_imageView release];
    
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *arr = [self.dicForTabViewCell objectForKey:@(indexPath.section)];
    ModelForTravelTabViewCell *model = [arr objectAtIndex:indexPath.row];
    
    if (![model.photo_1600 isEqualToString:@""]) {
        
        if ([model.text isEqualToString:@""]) {
            return 10 + [NSString heightForImageSetHeight:[[model.photo_info objectForKey:@"h"] floatValue] setWidth:[[model.photo_info objectForKey:@"w"] floatValue] getWidth:self.view.bounds.size.width - 20] + 10 + 15 + 10 + 10;
        }
        return 10 + [NSString heightForImageSetHeight:[[model.photo_info objectForKey:@"h"] floatValue] setWidth:[[model.photo_info objectForKey:@"w"] floatValue] getWidth:self.view.bounds.size.width - 20] + 10 + [NSString heightForLabel:model.text withWidth:self.view.bounds.size.width - 40 withEdge:0 withFontOfSize:15] + 10 + 15 + 10 + 10;
    }
    
    if ([model.text isEqualToString:@""]) {
        return 10 + 10 + 10 + 15 + 10;
    }
    return 10 + [NSString heightForLabel:model.text withWidth:self.view.bounds.size.width - 40 withEdge:0 withFontOfSize:15] + 10 + 10 + 10 + 15 + 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 30;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [[self.dicForTabViewCell objectForKey:@(section)] count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.arrForTabViewSectionHeader.count;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    ViewForTravelTabViewSectionHeader *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"poolForHeader"];
    view.contentView.backgroundColor = [UIColor clearColor];
    ModelForTravelTabViewSectionHeader *model = [self.arrForTabViewSectionHeader objectAtIndex:section];
    [view passData:model];
    
    return view;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CellForTravelTabView *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    [cell passData:[[self.dicForTabViewCell objectForKey:@(indexPath.section)] objectAtIndex:indexPath.row]];
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    
}

- (void)settingsNavigationController {
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-fanhui(baise)"] style:UIBarButtonItemStylePlain target:self action:@selector(houtuiAction:)] autorelease];
}

- (void)houtuiAction:(UIBarButtonItem *) button {
    [self.navigationController popViewControllerAnimated:YES];
    self.tabBarController.tabBar.hidden = NO;
}

- (void)handleData {
    self.arrForTabViewSectionHeader = [NSMutableArray array];
    self.dicForTabViewCell = [NSMutableDictionary dictionary];
    
    [NetWorkHandler networkingWithUrl:self.url comleteHandle:^(id result, NSData *data) {
        int count = 0;
        ModelForTravelTabViewHeader *model = [[ModelForTravelTabViewHeader alloc] init];
        [model setValuesForKeysWithDictionary:result];
        
        // 赋值地图照片和tabViewHeader
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:model.trackpoints_thumbnail_image]];
        self.viewForTravelTabViewHeader = [[ViewForTravelTabViewHeader alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 300)];
        [self.viewForTravelTabViewHeader passData:model];
        [model release];
        self.viewForTravelTabViewHeader.backgroundColor = [UIColor clearColor];
        self.tableView.tableHeaderView = self.viewForTravelTabViewHeader;
        [_viewForTravelTabViewHeader release];
        
        // 解析区头数据
        NSArray *arr = [result objectForKey:@"days"];
        
        for (NSDictionary *dic in arr) {
            ModelForTravelTabViewSectionHeader *modelForSectionHeader = [[ModelForTravelTabViewSectionHeader alloc] init];
            [modelForSectionHeader setValuesForKeysWithDictionary:dic];
            [self.arrForTabViewSectionHeader addObject:modelForSectionHeader];
            [modelForSectionHeader release];
            NSArray *arrForWaypoints = [dic objectForKey:@"waypoints"];
            NSMutableArray *arrForTemp = [NSMutableArray array];
            for (NSDictionary *dic in arrForWaypoints) {
                ModelForTravelTabViewCell *modelForCell = [[ModelForTravelTabViewCell alloc] init];
                [modelForCell setValuesForKeysWithDictionary:dic];
                [arrForTemp addObject:modelForCell];
                [modelForCell release];
            }
            [self.dicForTabViewCell setObject:arrForTemp forKey:@(count)];
           
            count++;
            
        }
        [self.tableView reloadData];
     
    }];
}
#define WIDTH self.view.bounds.size.width
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

     //下拉放大
    CGPoint point = scrollView.contentOffset;
    
    if (point.y < -264) {
        CGRect rect = self.imageView.frame;
        rect.origin.y = point.y;
        rect.size.height = -point.y;
        self.imageView.frame = rect;
    }
    
    CGRect rect = self.viewForTravelTabViewHeader.imageViewForAvatar_l.frame;
    if (scrollView.contentOffset.y < -64 && scrollView.contentOffset.y > -264) {
        CGFloat height = ABS(scrollView.contentOffset.y) - 64;
        self.viewForTravelTabViewHeader.imageViewForAvatar_l.frame = CGRectMake(rect.origin.x, rect.origin.y, WIDTH / 5 * (height / 200), WIDTH / 5 * (height / 200));
    } 
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
